<template>
  <div class="order">
    <div class="orderBox_l">
      <div class="orderBox_l_top">
        <div>酒店名称：{{ room.baseInfo.name }}</div>
        <div>
          床型：{{ info.room.bedGroups[0][0].bedDesc
          }}{{ info.room.bedGroups[0][0].bedType }}
        </div>
        <div>
          地址：{{ room.baseInfo.address.city.name
          }}{{ room.baseInfo.address.addressLine.name }}
        </div>
        <!-- <div>预定规则：2</div> -->
        <div>电话：{{ room.baseInfo.phone }}</div>
        <!-- <div>早餐：{{info.room.service.mealType[0].desc}}</div> -->
        <div>
          是否免费取消：{{
            info.cancelPolicy[0].cancelType == "0"
              ? "不可取消"
              : info.cancelPolicy[0].moveUpCancelDays == 0
              ? "当天" + info.cancelPolicy[0].moveUpCancelHour + "前"
              : "提前" + info.cancelPolicy[0].moveUpCancelDays + "天取消"
          }}
        </div>
        <div>
          有无窗：{{
            info.room.realroom.roomContents[0].baseInfo.window.hasWindow == "0"
              ? "有窗"
              : info.room.realroom.roomContents[0].baseInfo.window.hasWindow ==
                "1"
              ? "部分有窗"
              : info.room.realroom.roomContents[0].baseInfo.window.hasWindow ==
                "2"
              ? "无窗"
              : ""
          }}
        </div>
      </div>
      <div style="padding: 20px 0 0 10px">
        <h4>填写订单信息</h4>
        <div class="orderBox_l_dateBox">
          <div class="orderBox_l_dateBoxItem">
            入住日期:{{ conditions.arrivalDate }}
          </div>
          <div class="orderBox_l_dateBoxItem1">{{ days }}晚</div>
          <div>入住日期:{{ conditions.departureDate }}</div>
        </div>
        <div>住客信息</div>
        <span style="font-size: 12px"
          >请按照实际人数填写，姓名与证件保持一致</span
        >
        <div class="perressBox">
          <div style="margin: 20px 0">
            房间数量：
            <el-select
              @change="roomsNumberChange"
              size="mini"
              style="width: 140px"
              v-model="roomsNumber"
              placeholder="请选择"
            >
              <el-option
                v-for="item in roomsNumberList"
                :key="item.value"
                :label="item.value + '间'"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
          <div
            style="margin: 7px 0 10px 0"
            v-for="(item, index) in roomsPeopleList"
            :key="index"
          >
            入住人{{ index + 1 }}:
            <el-input
              size="mini"
              style="width: 130px"
              placeholder="请输入入住人名称"
              v-model="item.name"
            ></el-input>
          </div>
          <el-form
            inline
            :model="Form"
            size="mini"
            :rules="rules"
            ref="Form"
            class="demo-ruleForm"
          >
            <el-form-item label="联系人" prop="name" label-width="70px">
              <el-input
                v-model="Form.name"
                size="mini"
                style="width: 200px"
              ></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone" label-width="70px">
              <el-input
                v-model="Form.phone"
                size="mini"
                style="width: 200px"
              ></el-input>
            </el-form-item>

            <el-form-item label="最晚到店时间" prop="date" label-width="110px">
              <el-time-select
                size="mini"
                style="width: 200px"
                v-model="Form.date"
                :picker-options="{
                  start: '02:00',
                  step: '00:15',
                  end: '24:00',
                }"
                placeholder="选择时间"
              >
              </el-time-select>
            </el-form-item>

            <div>
              <el-checkbox v-model="fapiaoChoose">需要发票</el-checkbox>
            </div>
            <div v-if="fapiaoChoose == true">
              <div>如需发票，请向酒店索取</div>
            </div>
            <div class="zhifu">
              <div>
                <span>在线支付</span>
                <span style="font-size: 26px; color: #de7d4c"
                  >￥{{ totalPrice / 100 }}</span
                >
              </div>
              <div>
                <el-button type="primary" @click="submitForm('Form')"
                  >立即创建</el-button
                >
              </div>
            </div>
          </el-form>
        </div>
      </div>
    </div>
    <div class="orderBox_r">
      <div class="orderBox_r_top">
        <div>{{ roomsNumber }}间x{{ days }}晚</div>
        <div>￥{{ totalPrice / 100 }}</div>
      </div>
      <div class="orderBox_r_bot">
        <div class="orderBox_r_botIetm">
          总价格<span style="color: #de7d4c"
            >￥ <span style="font-size: 22px">{{ totalPrice / 100 }}</span>
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "order",
  data() {
    return {
      // 房间数
      roomsNumber: this.$route.params.theroomnumber,
      // 房间数量
      roomsNumberList: [
        {
          value: 1,
        },
        {
          value: 2,
        },
        {
          value: 3,
        },
        {
          value: 4,
        },
        {
          value: 5,
        },
        {
          value: 6,
        },
        {
          value: 7,
        },
        {
          value: 8,
        },
        {
          value: 9,
        },
        {
          value: 10,
        },
      ],
      //入住人列表
      roomsPeopleList: [],
      // 手机号
      Form: {
        name: "",
        phone: "",
        date: "",
      },
      rules: {
        name: [{ required: true, message: "请输入联系人", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        date: [{ required: true, message: "请选择", trigger: "change" }],
      },
      // 是否需要发票
      fapiaoChoose: false,
      // 酒店详细信息

      custmerInfo: this.$route.params.huiyuan,
      hotelId: this.$route.params.hotelId,
      info: this.$route.params.info,
      conditions: this.$route.params.conditions,
      theroomnumber: this.$route.params.theroomnumber,
      room: this.$route.params.room,
      // 天数
      days: null,
      totalPrice: null,
    };
  },
  props: {},
  components: {},
  methods: {
    //房间数量事件
    roomsNumberChange(val) {
      console.log(val);
      console.log(this.roomsNumber);
      this.roomsPeopleList = [];
      for (let i = 0; i < val; i++) {
        this.roomsPeopleList.push({
          name: "",
        });
      }
      console.log(this.roomsPeopleList);
      this.sumtotalprice();
    },
    // 创建订单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let istg = 0;
          for (let i = 0; i < this.roomsPeopleList.length; i++) {
            if (this.roomsPeopleList[i].name != "") {
              istg = 1;
            } else {
              istg = 0;
              this.$message({
                message: "请完整填写入住人",
                type: "warning",
              });
              break;
            }
          }
          if ((istg = 1)) {
            this.createOrder();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 下单
    createOrder() {
      var peopleStr = "";
      if (this.roomsPeopleList.length > 1) {
        for (let i = 0; i < this.roomsPeopleList.length; i++) {
          peopleStr += this.roomsPeopleList[i].name + ",";
        }
      } else {
        peopleStr = this.roomsPeopleList[0].name;
      }
      let dateStr = "";
      dateStr = this.conditions.arrivalDate + " " + this.Form.date + ":00";
      console.log(peopleStr);
      let obj = {
        hotelName: this.room.baseInfo.name,
        source: "Crm",
        dayNumber: this.days,
        // 房型
        // 其他金额
        otherAmount: 0,
        userId: this.custmerInfo.id,
        hotelId: this.hotelId,
        goodsId: this.info.productId,
        personNames: peopleStr,
        contactName: this.Form.name,
        contactPhone: this.Form.phone,
        arriveDate: dateStr,
        checkinDate: this.conditions.arrivalDate,
        checkoutDate: this.conditions.departureDate,
        roomNum: this.roomsNumber,
        totalPrice: this.totalPrice,
        settlePrice: this.totalPrice,
      };

      this.showLoading();

      this.$axios
        .createhotelOrder(obj)
        .then((res) => {
          this.closeLoading();
          console.log(res);
          if (res.code == "00000") {
            this.$router.push({
              name: "paya",
              params: {
                huiyuan: this.custmerInfo,
                data: res.data,
                // dfghdfd: this.dfghdfd,
                // hghghg: this.hghghg,
                // dfghdfdList: this.dfghdfdList,
                // price: this.infolist.price,
                // totalprice: this.totalprice,
                // fjcpprice: this.fjcpprice,
                // basePrice: this.basePricezong,
              },
            });
          }
        })
        .catch((err) => {
          this.closeLoading();
        });
    },
    // 计算天数
    lateness(sDate1, sDate2) {
      var dateSpan, tempDate, iDays;
      sDate1 = Date.parse(sDate1);
      sDate2 = Date.parse(sDate2);
      dateSpan = sDate2 - sDate1;
      dateSpan = Math.abs(dateSpan);
      iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
      return iDays;
    },
    // 计算酒店价格
    sumtotalprice() {
      let price = 0;
      for (let i = 0; i < this.info.inclusivePrice.length; i++) {
        price += this.info.inclusivePrice[i].salePrice;
      }
      this.totalPrice = price * this.roomsNumber;
    },
    closeLoading() {
      this.tableLoading.close();
    },
    showLoading() {
      this.tableLoading = this.$loading({
        text: "拼命加载数据中", //显示在加载图标下方的加载文案
        spinner: "el-icon-loading", //自定义加载图标类名
        background: "rgba(0, 0, 0, 0.4)", //遮罩层颜色
        target: document.querySelector("#table"), //loadin覆盖的dom元素节点
      });
    },
  },
  mounted() {
    console.log(this.$route.params);
    this.days = this.lateness(
      this.conditions.arrivalDate,
      this.conditions.departureDate
    );
    console.log(this.days);
    this.roomsNumberChange(this.$route.params.theroomnumber);
  },
};
</script>

<style scoped>
.order {
  margin: 30px 0 0 10px;
  display: flex;
}
.orderBox_l {
  width: 76%;
  height: 100%;
}
.orderBox_l_top {
  padding: 10px 0 0 10px;
  width: 95%;
  background-color: #f5f8f9;
}
.orderBox_l_dateBox {
  display: flex;
  margin: 20px 0;
}
.orderBox_l_dateBoxItem {
  width: 180px;
}
.orderBox_l_dateBoxItem1 {
  width: 130px;
  margin-left: 30px;
}
.orderBox_r {
  background-color: #f5f8f9;

  width: 22%;
  height: 100%;
}
.zhifu {
  width: 300px;
  margin-left: 20px;
  margin-top: 30px;
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.orderBox_r_top {
  display: flex;
  padding: 10px 15px;
  justify-content: space-between;
}
.orderBox_r_bot {
  margin-top: 60px;
}
.orderBox_r_botIetm {
  margin-left: 55%;
}
</style>